from Project.HTools.generate.qr.qr_code_panel import QRCodePanel
import qrcode
from io import BytesIO
import wx


class QRCodeMain(QRCodePanel):
    def __init__(self, parent):
        QRCodePanel.__init__(self, parent)

    def generate_qr_code(self, event):
        text = self.edit_qr_content.GetValue()
        if text == '':
            wx.MessageDialog(self, '内容不能为空', '操作提醒', wx.OK).ShowModal()
            return
        qr = qrcode.QRCode()
        qr.add_data(text)
        qr.make(fit=True)
        img = qr.make_image(fill_color="black", back_color="white")

        # 将PIL图像转换为wxPython图像
        buffer = BytesIO()
        img.save(buffer, format='PNG')
        buffer.seek(0)
        wx_image = wx.Image(buffer, wx.BITMAP_TYPE_PNG)
        wx_bitmap = wx_image.ConvertToBitmap()

        # 显示二维码
        self.qr_image.SetBitmap(wx_bitmap)
        self.Layout()
